@extends('layouts.adminlayout')

@section('main')

<p>{{ link_to_route('accounts.create','Thêm Tài Khoản Cán Bộ',null,array('class'=>'btn btn-info')) }}</p>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title">Tài khoản <small>Danh sách tài khoản</small></h3>
                </div><!-- /.box-header -->
                <div class="box-body table-responsive">
                    @if ($tai_khoans->count())
                    <table id="dataTable" class="table table-bordered table-striped" >
                        <thead>
                            <tr>
                                <th>Tài Khoản</th>
                                <th>Mã Cán Bộ</th>
                                <th>Họ Tên</th>
                                <th>Ngày Sinh</th>
                                <th>Giới Tính</th>
                                <th>Email</th>
                                <th>Chức Vụ</th>
                                <th>Phân Quyền</th>
                                <th>Sửa</th>
                                <th>Xóa</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th>Tài Khoản</th>
                                <th>Mã Cán Bộ</th>
                                <th>Họ Tên</th>
                                <th>Ngày Sinh</th>
                                <th>Giới Tính</th>
                                <th>Email</th>
                                <th>Chức Vụ</th>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach ($tai_khoans as $tai_khoan)
                            <tr>
                                <td>{{{ $tai_khoan->username }}}</td>
                                <td>{{{ $tai_khoan->mascb }}}</td>
                                <td>{{{ $tai_khoan->ho_cb }}} {{{ $tai_khoan->ten_cb }}}</td>
                                <td>{{{ $tai_khoan->ngay_sinh_cb}}}</td>
                                <td>{{{ $tai_khoan->gioi_tinh_cb }}}</td>
                                <td>{{{ $tai_khoan->email_cb }}}</td>
                                <td>{{{ $tai_khoan->chuc_vu }}}</td>
                                <td>
                                    <button type="button" name="btn_phan_quyen{{{$tai_khoan->id}}}" id="btn_phan_quyen{{{$tai_khoan->id}}}" class="btn btn-primary" onclick="phan_quyen({{{$tai_khoan->id}}})">Phân Quyền</button>
                                </td>
                                <td>
                                    {{ link_to_route('accounts.edit', '', array($tai_khoan->id), array('class' => 'btn btn-info fa fa-edit')) }}
                                </td>
                                <td> 
                                @if($tai_khoan->id != Auth::user()->id)
                                    {{ Form::open(array('method' => 'DELETE', 'route' => array('accounts.destroy', $tai_khoan->id),'class'=>'delete_tai_khoan')) }}
                                    <button type="submit" class="btn btn-danger"><li class="fa fa-trash-o"></li></button>
                                    {{ Form::close() }}
                                @endif
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                    @else
                        Không tồn tại tài khoản
                    @endif
                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
    <div class="modal fade" id="modal_permission" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header"><h3><b>Danh Sách Chức Năng</b></h3></center>
                </div>
                <div class="modal-body" id='body_modal' style="padding: 0px 0px 0px 60px;">

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Đóng</button>
                </div>
            </div>
        </div>
    </div>
<script src="<?php echo asset('public/admin/js/plugins/datatables/jquery.dataTables.columnFilter.js'); ?>" type="text/javascript"></script>
<script type="text/javascript">
    // Setup - add a text input to each footer cell
        $('#dataTable tfoot th').each(function() {
            var title = $('#dataTable thead th').eq($(this).index()).text();
            $(this).html('<input type="text" placeholder="' + title + '" />');
        });

        var table = $('#dataTable').DataTable({
                    "scrollX": "100%",
                    "scrollCollapse": true,
                    "paging": true,
                    "autoWidth": false
                });
        // Apply the filter
        table.columns().eq(0).each(function(colIdx) {
            $('input', table.column(colIdx).footer()).on('keyup change', function() {
                table
                        .column(colIdx)
                        .search(this.value)
                        .draw();
            });
        });
</script>
    <script type="text/javascript">
        function phan_quyen(id) {
                // $('#body_modal').html(data);
                $.ajax({
                url: '<?php echo asset("permissions/dsquyen") ?>',
                        type: 'post',
                        dataType: 'html',
                        data: {id: id},
                })
                        .done(function(data) {
                        $('#body_modal').html(data);
                                $('#modal_permission').modal('show');
                        })
                        .fail(function() {
                        console.log("error");
                        })
                        .always(function() {
                        console.log("complete");
                        });
                }

    function set_permission(id_tai_khoan, id_chuc_nang){
        $.ajax({
        url: '<?php echo asset("permissions/setpermission") ?>',
                type: 'post',
                dataType: 'json',
                data: {id_tai_khoan: id_tai_khoan,
                        id_chuc_nang: id_chuc_nang},
        })
                .done(function(data) {
                if (data == 0){

                }
                })
                .fail(function() {
                console.log("error");
                })
                .always(function() {
                console.log("complete");
                });
        }
        $('.delete_tai_khoan').submit(function(event) {
       var txt;
        var r = confirm("Xác nhận xóa?");
        if (r == true) {
            return true;
        } else {
            return false;
        }
        return false;
    });
    </script>
    @stop

